﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormProductionRecord : Form
    {
        public FormProductionRecord()
        {
            InitializeComponent();
        }

        private void FormProductionRecord_Load(object sender, EventArgs e)
        {
            InitGrid(grid1);

            var list = LoadingView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void InitGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;

            grid.Rows = 1;
            grid.Cols = 24 + Config.镀铜段数 * 4;

            grid.Row(0).Height = 50;

            //这个包围区间要减1 居中
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;

            grid.DisplayRowNumber = true;

            int col = 0;

            grid.Cell(0, col++).Text = "序号";
            grid.Cell(0, col++).Text = "上料时间";
            grid.Cell(0, col++).Text = "员工";
            grid.Cell(0, col++).Text = "批号";
            grid.Cell(0, col++).Text = "料号";
            grid.Cell(0, col++).Text = "宽度(mm)";
            grid.Cell(0, col++).Text = "C电流密度";
            grid.Cell(0, col++).Text = "S电流密度";
            grid.Cell(0, col++).Text = "镀铜时间";
            grid.Cell(0, col++).Text = "镀铜速度";
            grid.Cell(0, col++).Text = "C面厚度";
            grid.Cell(0, col++).Text = "S面厚度";
            grid.Cell(0, col++).Text = "板厚";
            grid.Cell(0, col++).Text = "电镀效率";
            grid.Cell(0, col++).Text = "卡板警报电压";
            grid.Cell(0, col++).Text = "除油频率";


            for (int i = 1; i <= Config.镀铜段数; i++)
            {
                grid.Cell(0, col++).Text = "铜" + i.ToString() + "段电流系数";
                grid.Cell(0, col++).Text = "铜" + i.ToString() + "段C面电流";
                grid.Cell(0, col++).Text = "铜" + i.ToString() + "段S面电流";
                grid.Cell(0, col++).Text = "铜" + i.ToString() + "段流量";
            }

            grid.Column(0).Width = 40;
            grid.Column(1).Width = 140;
            grid.Column(2).Width = 140;
            grid.Column(3).Width = 140;
            grid.Column(4).Width = 140;

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void UpdateGrid(FlexCell.Grid grid, List<LoadingView> list)
        {
            list.Sort((a, b) => b.Time.CompareTo(a.Time));

            grid1.AutoRedraw = false;
            grid1.Rows = list.Count + 1;

            for (int row = 1; row <= list.Count; row++)
            {
                var loading = list[row - 1];

                int col = 1;

                grid1.Cell(row, col++).Text = loading.Time.ToString();
                grid1.Cell(row, col++).Text = loading.Loader;
                grid1.Cell(row, col++).Text = loading.Batch;
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.Name;
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.Width.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.C面电流密度ASF.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.S面电流密度ASF.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.D镀铜时间.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.D镀铜速度.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.D镀铜C面厚度.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.D镀铜S面厚度.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.Thickness.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.D电流效率.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.K卡板报警电压.ToString();
                grid1.Cell(row, col++).Text = loading.LoadingRecipe.C除油1频率.ToString();

                for (int i = 0; i < KorbeRTR.Config.镀铜段数; i++)
                {
                    var slot = loading.LoadingRecipe.CuSlots[i];
                    grid1.Cell(row, col++).Text = slot.D电流系数.ToString();
                    grid1.Cell(row, col++).Text = slot.C面电流.ToString();
                    grid1.Cell(row, col++).Text = slot.S面电流.ToString();
                    grid1.Cell(row, col++).Text = slot.Flow.ToString();

                }

            }

            grid1.AutoRedraw = true;
            grid1.Refresh();
        }

        private void tsm今天_Click(object sender, EventArgs e)
        {

        }

        private void tsm这两天_Click(object sender, EventArgs e)
        {

        }

        private void tsm这三天_Click(object sender, EventArgs e)
        {

        }

        private void tsm本周_Click(object sender, EventArgs e)
        {

        }

        private void tsm本月_Click(object sender, EventArgs e)
        {

        }

        private void tsm全部_Click(object sender, EventArgs e)
        {

        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        private void DTP_Start_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                DTP_End.Value = DTP_Start.Value.Date.Date;
            }

            var list = LoadingView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void DTP_End_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                MessageBox.Show("查询时间不能超过7天");
                return;
            }

            var list = LoadingView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void tsExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsExport_Click(object sender, EventArgs e)
        {
            if (grid1.ExportToExcel("", true, true))
            {
                MessageBox.Show("输出成功", "导出Excel", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
